<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <el-form-item label="创建时间" prop="create_time">
                    <el-date-picker v-model="queryParams.create_time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px"> </el-date-picker>
                </el-form-item>
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-table ref="sortTable" v-loading="loading" :data="tableData" row-key="id" border script @sort-change="handleSortChange">
                <!-- <el-table-column label="SPU" prop="spu" width="120"> -->
                    <!-- <template slot-scope="scope">
                        <el-link type="primary" @click="showProductStockDialog(scope.row)">{{ scope.row.spu }}</el-link>
                    </template> -->
                <!-- </el-table-column> -->
                <el-table-column label="昨日采购单数" prop="buy_quantity" width=""></el-table-column>
                <el-table-column label="昨日到货单数" prop="arrive_quantity" width=""></el-table-column>
                <el-table-column label="最长缺货未发订单日期" prop="last_not_time" width=""></el-table-column>
                <el-table-column label="最长有货未发订单日期" prop="last_have_time" width=""></el-table-column>
                <el-table-column label="操作" min-width="" fixed="right">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="showDetailDialog(scope.row)">编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList" />
        </div>
        <detail-dialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" @success="handleQuery"></detail-dialog>
        <stock-list-dialog v-model="stockDialog.visible" v-if="stockDialog.visible" :params="stockDialog.data"></stock-list-dialog>
    </div>
</template>
<script>
import { getStatementStatisticsList } from '@/api/product'
import { parseTime } from '@/utils/quickadmin'
import DetailDialog from '../../components/StatementStatisticsDetailDialog.vue'
export default {
    name: 'ProductInventoryList',
    components: { DetailDialog },
    data() {
        return {
            // 遮罩层
            loading: true,
            download: false,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            // data中定义Arr数组，用来存放筛选列
            orderArray: [],
            sorts: [], // 用于存储多列排序信息的数组

            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                seller_sku: undefined,
                spu: undefined,
                create_time: [],
            },
            detailDialog: {
                visible: false,
                data: {},
            },
            stockDialog: {
                visible: false,
                data: {},
            },
        }
    },
    async created() {
        this.loading = true
        try {
            const attrData = await getAttrList()
            this.attrList = attrData?.data ?? []
            const purchaseLevelData = await getPurchaseLevel()
            this.purchaseTypeList = purchaseLevelData?.data ?? []
            const supplyData = await getSupplyList()
            this.supplyList = supplyData?.data ?? []
        } catch (error) {
            this.loading = false
        }
        this.getList()
    },
    methods: {
        resetSort() {
            this.queryParams.inventory_quantity = undefined
            this.queryParams.on_way_quantity = undefined
            this.queryParams.not_send_quantity = undefined
            this.queryParams.all_inventory_quantity = undefined
            this.queryParams.purchase_number_num = undefined
        },
        handleSortChange({ column, prop, order }) {
            this.resetSort()
            this.queryParams[prop] = order === 'descending' ? 'desc' : order === 'ascending' ? 'asc' : undefined
            this.getList()
        },
        showProductStockDialog(row) {
            this.stockDialog.visible = true
            this.stockDialog.data = row
        },
        showDetailDialog(row) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
        },

        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            getStatementStatisticsList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')
            this.resetSort()
            this.$refs.sortTable.clearSort()
            this.handleQuery()
        },

         //更新排序
        updateSort() {
            this.$confirm(`预计等待一分钟`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.baseRequest_updateSort({ })
                })
                .catch(err => {})
        },
        baseRequest_updateSort({ }) {
            this.request
                .post("/product/ProductInventory/updateSort", { })
                .then(response => {
                    console.log('response',response);
                    
                    if(response.code == 200){
                        this.$message.success('操作成功');
                    }
                    console.log('test',13);
                    
                    this.getList()
                })
                .finally(() => {
                })
        },
    },
}
</script>
<style>
.total-warp {
    font-size: 18px;
    font-weight: bold;
    padding: 8px 0 16px 0;
}
</style>
